////////////////////////////////////////////////////////////////////////////////
// Copyright 2005 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   TASK
// AUTHOR           :   Ric Yik
// DATE CREATE      :   2005/12/29
// DESCRIPTION      :   a MAINTENANCE ACTION file for the function, TASK
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2005/12/29   Ric Yik         1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.taskMaster.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.setup.taskMaster.pk.TaskHdrPk;
import com.erp.app.setup.taskMaster.schema.TaskHdrSchema;
import com.erp.app.setup.taskMaster.vo.TaskHdrVo;
import com.erp.app.util.web.ActionErrorUtil;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.action.SimpleMaintStyleAction;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;

/**
* A <strong>Maintenance Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the user document maintenance events
* from the web browsers.
*
* @author          Ric Yik
* @version 1.0     2005/12/29
* @since           FrameWork 1.0
*
*/
public class TaskMaintenanceAction extends SimpleMaintStyleAction {

    ////////////////////////////////////////////////////////////////////////////////
    // CONSTANTS DEFINITION
    ////////////////////////////////////////////////////////////////////////////////

    protected static final String FIELD_DTLTASKDESC = "DtlTaskDesc";

    protected static final String ACTION_ADDDTL = ACTION_ADD_DTL;
    public static final String ACTION_CHANGE_FOLDER_HDR = ACTION_CHANGE_FOLDER + "Hdr";
    public static final String ACTION_CHANGE_FOLDER_DTL = ACTION_CHANGE_FOLDER + "Dtl";

    protected static final String ACTION_PEDIT = "pEdit";
    protected static final String STATUS_PSET = "sPSet";

    public static final int TAB_IDX_DTL = 0;
    public static final int TAB_IDX_HDR = -1;

    ////////////////////////////////////////////////////////////////////////////////
    // GLOBAL VARIABLES DEFINITION
    ////////////////////////////////////////////////////////////////////////////////



    ////////////////////////////////////////////////////////////////////////////////
    // LOGICAL AND BUSINESS METHODS
    ////////////////////////////////////////////////////////////////////////////////

    public ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {
            if (!isHandled(request) && action.startsWith(ACTION_PEDIT)) {
                actionForward = processActionPEdit(mapping, formIn, request, response, errors);
                setHandled(request);
            } else if (!isHandled(request) && action.equalsIgnoreCase(ACTION_ADDDTL)) {
                actionForward = processActionAddDtl(mapping, formIn, request, response, errors);
                saveToken(request);
                setHandled(request);
            }
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }

        return actionForward;
    }

    protected ActionForward processOtherStatus(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response,
        ActionErrors errors, String rb_status) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {
            if (rb_status.startsWith("pSet")) {
                return processStatusPSet(mapping, formIn, request, response, errors, rb_status);
            }
        } catch (Exception e) {
            Logger.log(this, e);
        }

        return actionForward;
    }

    protected ActionForward processActionPEdit(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        HttpSession session = request.getSession(false);
        ActionForward actionForward = new ActionForward(mapping.getInput());

        String action = CommonUtil.nullToString(form.getPageEvent());
        LinkedList keyList = new LinkedList();
        keyList.add("Type Code");
        keyList.add(form.getDocType());

        String field = action.substring(ACTION_PEDIT.length());
        String eventSrc = form.getPageEventSrc();
        String fieldName = "";
        String fieldValue = "";
        boolean readOnly = false;
        int selectedIndex = form.getPageEventSrcIdx();

        if (field.equalsIgnoreCase(FIELD_DTLTASKDESC)) {
            ArrayList rows = form.getMultiRows(TAB_IDX_DTL);
            TaskDtlRow row = (TaskDtlRow) rows.get(selectedIndex);

            keyList.add("Task Code");
            keyList.add(row.getTaskCode());

            fieldName = "Task Description";
            fieldValue = row.getDtlTaskDesc();
        }

        HashMap params = new HashMap();
        params.put(ProjectConstants.SESSION_SESSION_VALUE, getSessionSimpleMaintStatus());
        params.put(ProjectConstants.SESSION_OPENER_PATH, getFunctionId());
        params.put(ProjectConstants.SESSION_KEY_LIST, keyList);
        if (readOnly) params.put(ProjectConstants.SESSION_SIMPLE_READ_ONLY, ProjectConstants.GENERIC_YES);
        params.put(ProjectConstants.SESSION_FIELD, field);
        params.put(ProjectConstants.SESSION_EVENTSRC, eventSrc);
        params.put(ProjectConstants.SESSION_EDIT_COLUMN_NAME, fieldName);
        params.put(ProjectConstants.SESSION_EDIT_VALUE, fieldValue);
        session.setAttribute(ProjectConstants.SESSION_REMARK_TEXT_PARAMETERS, params);

        return actionForward;
    }

    protected ActionForward processStatusPSet(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors, String rb_status) {
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        HttpSession session = request.getSession(false);
        ActionForward actionForward = new ActionForward(mapping.getInput());

        HashMap params = (HashMap) session.getAttribute(ProjectConstants.SESSION_REMARK_TEXT_PARAMETERS);
        session.removeAttribute(ProjectConstants.SESSION_REMARK_TEXT_PARAMETERS);

        // sets the remark text
        String field = (String) params.get(ProjectConstants.SESSION_FIELD);
        int selectedIndex = Integer.parseInt((String) params.get(ProjectConstants.SESSION_EVENTSRC));
        String editValue = (String) params.get(ProjectConstants.SESSION_EDIT_VALUE);

        if (field.equals(FIELD_DTLTASKDESC)) {
            ArrayList rows = form.getMultiRows(TAB_IDX_DTL);
            if (rows == null || rows.size() < selectedIndex) return actionForward;
            ((TaskDtlRow) rows.get(selectedIndex)).setDtlTaskDesc(editValue);
        }
        return actionForward;
    }

    protected ActionForward processActionAddDtl(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        String action = CommonUtil.nullToString(form.getAction());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        ArrayList dtlList = form.getMultiRows(TaskMaintenanceAction.TAB_IDX_DTL);
//        if(CommonUtil.nullToString(form.getDocType()).equals(ProjectConstants.DOC_TYPE_FABRICS)){
//            if(dtlList.size()>=10){
//
//                ActionErrorUtil.addErrorForSpecficPurpose(errors,"When Doc Type is FABRICS Only 10 Detail Rows Allowed");
//                return actionForward;
//            }
//        } else if(CommonUtil.nullToString(form.getDocType()).equals(ProjectConstants.DOC_TYPE_TRIM)){
//            if(dtlList.size()>=13){
//
//                ActionErrorUtil.addErrorForSpecficPurpose(errors,"When Doc Type is TRIM Only 13 Detail Rows Allowed");
//                return actionForward;
//            }
//        }
        try {
            TaskDtlRow rowToAdd = new TaskDtlRow();
            form.setCurrIdx(0);
            rowToAdd.setCompanyCode(form.getCompanyCode());
            rowToAdd.setDocType(form.getDocType());
            rowToAdd.setDtlStatus(ProjectConstants.GENERIC_ACTIVE);
            form.addRow(rowToAdd);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }


        return actionForward;
    }

    // ----------------------------------------------------------------------------//
    // add your new business/logical methods/functions HERE
    //----------------------------------------------------------------------------//


    ////////////////////////////////////////////////////////////////////////////////
    // END OF LOGICAL AND BUSINESS METHODS
    ////////////////////////////////////////////////////////////////////////////////


    ////////////////////////////////////////////////////////////////////////////////
    // OVERRIDES PARENT'S OPTIONAL METHODS
    ////////////////////////////////////////////////////////////////////////////////

    protected void initAction(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        super.initAction(mapping, formIn, request, response, errors);

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void initForm(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        super.initForm(mapping, formIn, request, response, errors);

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void initPage(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        super.initPage(mapping, formIn, request, response, errors);

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void beforeCreate(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {
            form.setCompanyCode(WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_TASK));
            form.setStatus(ProjectConstants.GENERIC_ACTIVE);
            form.setTs(new TaskHdrSchema());
            form.setTsName("com.erp.app.setup.taskMaster.schema.TaskHdrSchema");
            form.setDcInd(GENERIC_NO);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void beforeSave(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void afterSave(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void beforeEdit(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void afterEdit(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void beforeDelete(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void afterDelete(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void beforeRefresh(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    protected void afterRefresh(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        String action = form.getPageEvent();
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    public String getSessionMaintStatus() {

        return getSessionSimpleMaintStatus();
    }

    public String getSessionMaintPk() {

        return getSessionSimpleMaintPk();
    }

    public String getStrutsId() {

        return getFunctionId();
    }

    protected String getSimpleSrchMenuId() {
        return ProjectConstants.MOD_TASK;
    }

    ////////////////////////////////////////////////////////////////////////////////
    // END OF OPTIONAL METHODS
    ////////////////////////////////////////////////////////////////////////////////


    ////////////////////////////////////////////////////////////////////////////////
    // IMPLEMENTS PARENT'S ABSTRACT METHODS
    ////////////////////////////////////////////////////////////////////////////////

    protected String getModuleName(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request,HttpServletResponse response) {
        return ProjectConstants.MOD_TASK;
    }

    protected String getFunctionId() {

        //struts-config xxxxx.do
        return "taskMaintenance.do";
    }

    protected Class getMasterFormClass() {

        //the corresponding form class
        return TaskMaintenanceForm.class;
    }

    protected Class getMasterVOClass() {

        //corresponding master vo class
        return TaskHdrVo.class;
    }

    protected String getMasterFormSessionName() {

        return "taskMaintenanceForm";
    }

    protected Class[] getChildRowClass() {

        Class[] childRowClass = new Class[1];

        childRowClass[0] = com.erp.app.setup.taskMaster.web.TaskDtlRow.class;

        return childRowClass;
    }

    protected String[] getFolderChildIdxMapping() {

        String[] folderChildIdx = new String[2];

        folderChildIdx[0] = String.valueOf(TAB_IDX_DTL);
        folderChildIdx[1] = String.valueOf(TAB_IDX_DTL);

        return folderChildIdx;
    }

    protected SimpleBasePk getPk(ActionForm formIn, HttpServletRequest request) {

        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        TaskHdrPk pk = null;
        HttpSession session = request.getSession(false);

        //TODO: Add the following line to ProjectSecurityConstants.java
        //public static final String SP_SHARE_TASK = "SP_SHARE_TASK";
        String companyCode = WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_TASK);

//		edit the following with appropriate form field names
        pk = new TaskHdrPk(companyCode, form.getDocType());
        return pk;
    }

    protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
        HttpServletResponse response, ActionErrors errors){

        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;
        form.setFormType(ProjectConstants.FORM_TYPE_MAINT);
    }



    ////////////////////////////////////////////////////////////////////////////////
    // END OF ABSTRACT METHODS
    ////////////////////////////////////////////////////////////////////////////////
}
